import { h, ref } from 'vue';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import { validate } from '@/utils/validateUtil';
import { renderOptionTag } from '@/utils';
import { useDictStore } from '@/store/modules/dict';

const dict = useDictStore();

export class State {
  public id = 0; // id
  public activityId = 0; // 活动id
  public name = ''; // 姓名
  public number = ''; // 参赛号
  public phone = ''; // 手机号
  public area = ''; // 地区
  public pb = ''; // PB
  public target = ''; // 目标
  public idCard = ''; // 身份证号
  public createAt = ''; // 添加时间
  public updateAt = ''; // 更新时间
  public status = 1; // 状态

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  activityId: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入活动id',
  },
  name: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入姓名',
  },
  idCard: {
    required: false,
    trigger: ['blur', 'input'],
    type: 'string',
    validator: validate.idCard,
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'name',
    component: 'NInput',
    label: '姓名',
    componentProps: {
      placeholder: '请输入姓名',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'number',
    component: 'NInput',
    label: '参赛号',
    componentProps: {
      placeholder: '请输入参赛号',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'idCard',
    component: 'NInput',
    label: '身份证号',
    componentProps: {
      placeholder: '请输入身份证号',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'status',
    component: 'NSelect',
    label: '状态',
    defaultValue: null,
    componentProps: {
      placeholder: '请选择状态',
      options: dict.getOption('sys_normal'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'activityTitle',
    component: 'NInput',
    label: '活动名称',
    componentProps: {
      placeholder: '请输入活动名称',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: 'id',
    key: 'id',
    align: 'left',
    width: -1,
  },
  {
    title: '姓名',
    key: 'name',
    align: 'left',
    width: -1,
  },
  {
    title: '参赛号',
    key: 'number',
    align: 'left',
    width: -1,
  },
  {
    title: '手机号',
    key: 'phone',
    align: 'left',
    width: -1,
  },
  {
    title: '地区',
    key: 'area',
    align: 'left',
    width: -1,
  },
  {
    title: 'PB',
    key: 'pb',
    align: 'left',
    width: -1,
  },
  {
    title: '目标',
    key: 'target',
    align: 'left',
    width: -1,
  },
  {
    title: '身份证号',
    key: 'idCard',
    align: 'left',
    width: -1,
  },
  {
    title: '添加时间',
    key: 'createAt',
    align: 'left',
    width: -1,
  },
  {
    title: '更新时间',
    key: 'updateAt',
    align: 'left',
    width: -1,
  },
  {
    title: '状态',
    key: 'status',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('sys_normal', row.status);
    },
  },
  {
    title: '活动名称',
    key: 'activityTitle',
    align: 'left',
    width: -1,
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['sys_normal']);
}